#!/usr/bin/env bash
# Summary: Display prefix for a Java version
# Usage: jenv prefix [<version>]
#
# Displays the directory where a Java version is installed. If no
# version is given, `jenv prefix' displays the location of the
# currently selected version.

set -e
[ -n "$JENV_DEBUG" ] && set -x

# Provide jenv completions
if [ "$1" = "--complete" ]; then
  echo system
  exec jenv-versions --bare
fi

if [ -n "$1" ]; then
  export JENV_VERSION="$1"
elif [ -z "$JENV_VERSION" ]; then
  JENV_VERSION="$(jenv-version-name)"
fi

if [ "$JENV_VERSION" = "system" ]; then
  JAVA_PATH="$(jenv-which java)"
  JAVA_PATH="${JAVA_PATH%/*}"
  echo "${JAVA_PATH%/bin}"
  exit
fi

JENV_PREFIX_PATH="${JENV_ROOT}/versions/${JENV_VERSION}"
if [ ! -d "$JENV_PREFIX_PATH" ]; then
  echo "jenv: version \`${JENV_VERSION}' not installed" >&2
  exit 1
fi

echo "$JENV_PREFIX_PATH"
